#!/bin/bash

current_time=`adb shell date "+%y_%m_%d_%H_%M_%S"`
mkdir $current_time
#exec 1>./$current_time/cpu_gpu_info_$current_time.csv

#set gpu
adb shell "echo performance > /sys/class/kgsl/kgsl-3d0/devfreq/governor"
echo "gpu governor: `adb shell cat /sys/class/kgsl/kgsl-3d0/devfreq/governor`" &> ./$current_time/cpu_gpu_info_$current_time.csv
#set cpu work mode
for i in {0..7}
do
	adb shell "echo performance > /sys/devices/system/cpu/cpu${i}/cpufreq/scaling_governor"
	echo "cpu${i} governor: `adb shell cat /sys/devices/system/cpu/cpu${i}/cpufreq/scaling_governor`" &>> ./$current_time/cpu_gpu_info_$current_time.csv
done
#adb logcat
adb logcat -c
adb logcat > ./${current_time}/${current_time}_logcat.log &

performence_test_tool_pid=`adb shell ps | gawk '/.*(com\.htc\.wavevr\.platform_perf_certif_tool)$/{print $2}'`
echo "`adb shell ps -o USER,PID,PPID,TID,TTY,VSZ,%VSZ,RSS,WCHAN,PCY,SCHED,ADDR,S,NAME -p $performence_test_tool_pid`" &>> ./$current_time/cpu_gpu_info_$current_time.csv

#grap systrace info
device_serial=`adb devices | gawk '/.*(device)$/{print $1}'`
category="gfx view sched freq idle bionic"
python systrace.py -o ./$current_time/systrace_$current_time.html --time=30 --buf-size=20480 --app="com.htc.wavevr.platform_perf_certif_tool" --serial="$device_serial" $category &

echo "time,foreground_cpu,top_cpu,background_cpu,cpu_index,cpu_max_freq,cpu_min_freq,cpu_current_freq,cpu_available_freqs,gpu_clock,gpu_max_clock,gpu_busy" &>> ./$current_time/cpu_gpu_info_$current_time.csv
isToRunning=true
trap "isToRunning=false" SIGINT

while $isToRunning
do
	nowtime=`adb shell date "+%y_%m_%d_%H_%M_%S"`
	fgcpu=`adb shell cat /dev/cpuset/foreground/cpus`
	topcpu=`adb shell cat /dev/cpuset/top-app/cpus`
	bgcpu=`adb shell cat /dev/cpuset/background/cpus`
	gpuclk=`adb shell cat /sys/class/kgsl/kgsl-3d0/gpuclk`
	gpumaxclk=`adb shell cat /sys/class/kgsl/kgsl-3d0/max_gpuclk`
	gpubusy=`adb shell cat /sys/class/kgsl/kgsl-3d0/gpubusy`
	
	for i in {0..7}
	do
		cpumaxfreq=`adb shell cat /sys/devices/system/cpu/cpu${i}/cpufreq/scaling_max_freq`
		cpuminfreq=`adb shell cat /sys/devices/system/cpu/cpu${i}/cpufreq/scaling_min_freq`
		cpucurfreq=`adb shell cat /sys/devices/system/cpu/cpu${i}/cpufreq/scaling_cur_freq`
		cpu_av_freq=`adb shell cat /sys/devices/system/cpu/cpu${i}/cpufreq/scaling_available_frequencies`
		echo "$nowtime,$fgcpu,$topcpu,$bgcpu,cpu${i},$cpumaxfreq,$cpuminfreq,$cpucurfreq,$cpu_av_freq$gpuclk,$gpuclk,$gpumaxclk,$gpubusy" &>> ./$current_time/cpu_gpu_info_$current_time.csv
	done
done
